<template>
  <div class="mytab">
    <!-- 自定义tab -->
    <div
      v-for="item in list"
      class="mytab-item pointer mx-3"
      :key="item.value"
      :class="{ 'is-active': item.value === autoActive }"
      @click="choose(item)"
    >
      <el-badge :value="item.num" style="font-weight: 700">
        {{ item.name }}
      </el-badge>
      <div class="active-line" v-if="item.value === autoActive"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'

// 定义props
const props = defineProps({
  list: {
    type: Array,
    required: true,
    default: () => [],
  },
  activeName: {
    type: Number,
    required: true,
    default: '',
  },
  showIcon: {
    type: Boolean,
    default: false,
  },
})

// 定义emits
const emit = defineEmits(['handleTab'])

// 响应式变量
const autoActive = ref('')

// 方法定义
const choose = (val) => {
  autoActive.value = val.value
  emit('handleTab', { activeName: val.value, menuid: val.menuid })
}

const setCurrentTab = (val) => {
  autoActive.value = val
}

// 生命周期钩子
onMounted(() => {
  autoActive.value = props.activeName
})

// 监听props变化
watch(
  () => props.activeName,
  (val) => {
    autoActive.value = val
  },
  { immediate: true }
)

// 暴露方法给父组件
defineExpose({
  setCurrentTab,
})
</script>

<style lang="scss" scoped>
.mytab {
  height: 36px;
  display: flex;
  align-items: center;
  cursor: default;

  .mytab-item:first-of-type {
    margin-left: 0;
  }
}

.mytab-item {
  padding: 0 20px;
  height: 38px;
  line-height: 38px;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #999999;
  cursor: pointer;
  position: relative;
  width: unset;
}

.is-active {
  padding: 0 20px;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #4c73c5 !important;
  font-weight: 700;
}

.active-line {
  min-width: unset;
  height: 4px;
  background-color: #4c73c5;
  box-shadow: 0px 2px 5px rgba(49, 104, 225, 0.35);
  border-radius: 2px;
  position: absolute;
  bottom: 0px;
  left: 0px;
  right: 0px;
}
</style>
